home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / comm / bbs / s342q07.lha / netstat.c < prev    next >
C/C++ Source or Header  |  1995-01-26  |  7KB  |  252 lines

  1. /**
  2.   Network Statistics Version 1.02:
  3.  
  4.   Input:
  5.       Datafile created by Citadel
  6.       cc       1    1     2    2 2 3      3 34       4 5        5
  7.       1   5 7  0    5     0    6 8 0      7 90       8 0        9
  8.       MMMYY xxxxxxxxxxxxxxxxxxxx nnnnnnnnnn nnnnnnnnnn nnnnnnnnnn
  9.             Name of Net Partner  chars in   chars out    time
  10.  
  11.   Name of datafile is 1st argument.
  12.   Optional arguments:
  13.    flag
  14.      -S    to report by system.
  15.      -M    to report totals on a monthly basis.
  16.  
  17.   Output:
  18.       Report
  19.  
  20. **/
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <dos.h>
  25. #include <math.h>
  26.  
  27. struct bbs
  28.   {
  29.   struct bbs *next;     /* next bbs system */
  30.   char name[21];        /* name of bbs */
  31.   long net_count[12];   /* number of network sessions */
  32.   long chars_in[12];    /* monthly totals of characters input */
  33.   long chars_out[12];   /* monthly totals of characters output */
  34.   long time_used[12];   /* monthly totals of time used in networking */
  35.   long min_cps[12];         /* minimum cps that occured */
  36.   long max_cps[12];         /* maximum cps that occured */
  37.   };
  38.  
  39. struct bbs *systems = NULL; /* list of systems */
  40. int sflag = FALSE;      /* no individual per system statistics */
  41. int mflag = FALSE;      /* no monthy statistics */
  42. int lineno= 0;          /* current line number in the file */
  43. void Collect_Statistics(FILE *ip);
  44. void Report_Statistics(void);
  45. int main(int argc, char *argv[]);
  46. struct bbs *Find_System(char *name);
  47. void Report_BBS(struct bbs *ptr);
  48. int Date_to_Index(char *date);
  49.  
  50.  
  51. int main(int argc, char *argv[])
  52.   {
  53.   FILE *ip;
  54.   char *name=NULL;
  55.   printf("   Citadel Networking Statistics V1.02\n");
  56.   while( --argc )
  57.     {
  58.     if( argv[argc][0] == '-')
  59.       {
  60.       if( argv[argc][1] == 'S') sflag = TRUE;
  61.       if( argv[argc][1] == 's') sflag = TRUE;
  62.       if( argv[argc][1] == 'M') mflag = TRUE;
  63.       if( argv[argc][1] == 'm') mflag = TRUE;
  64.       }
  65.      else name = argv[argc];
  66.      };
  67.   if( name == NULL)
  68.     {
  69.     fprintf(stderr,"Error, need a file to process\n");
  70.     return(100);
  71.     };
  72.   if( (ip=fopen(name,"r")) == NULL )
  73.     {
  74.     fprintf(stderr," Error opening %s for input\n",argv[argc]);
  75.     return(100);
  76.     };
  77.   Collect_Statistics(ip);
  78.   fclose(ip);
  79.   Report_Statistics();
  80.   return 0;
  81.   }
  82.  
  83. void Collect_Statistics(FILE *ip)
  84.   {
  85.   struct bbs *ptr;
  86.   char line[80];
  87.   int did;
  88.   while( fgets(line, sizeof(line), ip) )
  89.     {
  90.     long  nci, nco, ntm;
  91.     lineno++;
  92.     line[ 3] = '\0';
  93.     line[27] = '\0';
  94.     line[38] = '\0';
  95.     line[49] = '\0';
  96.     line[60] = '\0';
  97.     did  = Date_to_Index(&line[ 0]);
  98.     ptr = Find_System(&line[ 7]);
  99.     ptr->net_count[did]++;
  100.     ptr->chars_in[did]  += (nci = atol(&line[28]));
  101.     ptr->chars_out[did] += (nco = atol(&line[39]));
  102.     ptr->time_used[did] += (ntm = atol(&line[50]));
  103.     nci += nco;
  104.     if( ntm > 0 )
  105.       {
  106.       nco = nci / ntm;
  107.       if( nco < ptr->min_cps[did]) ptr->min_cps[did] = nco;
  108.       if( nco > ptr->max_cps[did]) ptr->max_cps[did] = nco;
  109.       };
  110.     };
  111.   }
  112.  
  113. char *month[12] =
  114.   {
  115.   "jan", "feb", "mar", "apr", "may", "jun",
  116.   "jul", "aug", "sep", "oct", "nov", "dec"
  117.   };
  118.  
  119.  
  120. int Date_to_Index(char *date)
  121.   {
  122.   int i;
  123.   if( ! mflag )return 0;  /* if not doing monthly stats, skip this */
  124.   for( i=0; i<12; i++)
  125.     {
  126.     if( stricmp(month[i],date)== 0 )return i;
  127.     };
  128.   fprintf(stderr,"Error: INVALID DATE at line %d\n",lineno);
  129.   return 0;
  130.   }
  131.  
  132. struct bbs *Find_System(char *name)
  133.   {
  134.   struct bbs *ptr=systems;
  135.   int i;
  136.   if( sflag )
  137.     {
  138.     while ( ptr != NULL )
  139.       {
  140.       if( strcmp(ptr->name,name) == 0)return ptr;
  141.       ptr = ptr->next;
  142.       };
  143.     if( ptr == NULL )
  144.       {
  145.       ptr = calloc(1, sizeof(struct bbs));
  146.       strcpy(ptr->name,name);
  147.       for(i=0; i<12; i++)
  148.         {
  149.         ptr->min_cps[i] = 99999;
  150.         ptr->max_cps[i] = -1;
  151.         };
  152.       ptr->next    = systems;
  153.       systems      = ptr;
  154.       };
  155.     }
  156.   else
  157.     {
  158.     if( systems == NULL )
  159.       {
  160.       ptr = systems = calloc(1, sizeof(struct bbs));
  161.       strcpy(ptr->name," *** All Systems ***");
  162.       for(i=0; i<12; i++)
  163.         {
  164.         ptr->min_cps[i] = 99999;
  165.         ptr->max_cps[i] = -1;
  166.         };
  167.       };
  168.     };
  169.   return ptr;
  170.   }
  171.  
  172. void Report_Statistics(void)
  173.   {
  174.   long hr, sec, mins, avr_cps;
  175.   struct bbs *ptr=systems;
  176.   printf("   ----- Name -----     Session     Input     Output     Time          CPS\n");
  177.   printf("                         count                         HH:MM:SS   Min  Avr  Max\n");
  178.   Report_BBS(ptr);
  179.   if( sflag )
  180.     {
  181.     hr  = ptr->time_used[0];
  182.     sec = hr % 60;
  183.     hr /= 60;
  184.     mins = hr %60;
  185.     hr /= 60;
  186.     if( ptr->time_used[0] != 0)
  187.       {
  188.       avr_cps = (ptr->chars_in[0] + ptr->chars_out[0] ) / ptr->time_used[0];
  189.       }
  190.     else avr_cps = 0;
  191.     printf("%23s-------  ---------  ---------   --------\n"," ");
  192.     printf(" %20s  %7ld %10ld %10ld  %3ld:%02ld:%02ld       %4ld\n",
  193.     " ", ptr->net_count[0], ptr->chars_in[0], ptr->chars_out[0], hr, mins, sec
  194.     ,  avr_cps);
  195.     };
  196.   }
  197.  
  198. void Report_BBS(struct bbs *ptr)
  199.   {
  200.   int idx;
  201.   long avr_cps;
  202.   long hr, mins, sec;
  203.   if( ptr->next != NULL) Report_BBS(ptr->next);
  204.   for( idx=0; idx < 12 && mflag; idx++)
  205.     {
  206.     if( ptr->time_used[idx] == 0)continue;
  207.     hr  = ptr->time_used[idx];
  208.     sec = hr % 60;
  209.     hr /= 60;
  210.     mins = hr %60;
  211.     hr /= 60;
  212.     avr_cps = (ptr->chars_in[idx] + ptr->chars_out[idx] ) / ptr->time_used[idx];
  213.     printf(" %20s  %7ld %10ld %10ld  %3ld:%02ld:%02ld  %4ld %4ld %4ld\n",
  214.       month[idx], ptr->net_count[idx], ptr->chars_in[idx], ptr->chars_out[idx], hr, mins, sec,
  215.       ptr->min_cps[idx], avr_cps, ptr->max_cps[idx]);
  216.     if( idx > 0)
  217.       {
  218.       ptr->net_count[0] += ptr->net_count[idx];
  219.       ptr->chars_in[0]  += ptr->chars_in[idx];
  220.       ptr->chars_out[0] += ptr->chars_out[idx];
  221.       ptr->time_used[0] += ptr->time_used[idx];
  222.       if( ptr->min_cps[0] > ptr->min_cps[idx]) ptr->min_cps[0] = ptr->min_cps[idx];
  223.       if( ptr->max_cps[0] < ptr->max_cps[idx]) ptr->max_cps[0] = ptr->max_cps[idx];
  224.       };
  225.     };
  226.  
  227.   hr  = ptr->time_used[0];
  228.   sec = hr % 60;
  229.   hr /= 60;
  230.   mins = hr %60;
  231.   hr /= 60;
  232.   if( ptr->time_used[0] != 0)
  233.     {
  234.     avr_cps = (ptr->chars_in[0] + ptr->chars_out[0] ) / ptr->time_used[0];
  235.     }
  236.   else avr_cps = 0;
  237.   if( mflag  )
  238.     printf("%23s-------  ---------  ---------   --------\n"," ");
  239.   printf(" %20s  %7ld %10ld %10ld  %3ld:%02ld:%02ld  %4ld %4ld %4ld\n",
  240.   ptr->name, ptr->net_count[0], ptr->chars_in[0], ptr->chars_out[0], hr, mins, sec
  241.   , ptr->min_cps[0], avr_cps, ptr->max_cps[0]);
  242.   if( mflag )printf("\n");
  243.   if( ptr->next != 0 && sflag )
  244.       {
  245.       ptr->chars_in[0]  += ptr->next->chars_in[0];
  246.       ptr->chars_out[0] += ptr->next->chars_out[0];
  247.       ptr->time_used[0] += ptr->next->time_used[0];
  248.       ptr->net_count[0] += ptr->next->net_count[0];
  249.       };
  250.  
  251.   }
  252.